<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.transcargo.users.Contact" %>
<%@ page import="com.transcargo.users.User" %>
<%@ page import="com.transcargo.users.Transport" %>
<%@ page import="com.transcargo.geo.Country" %>
<%@ page import="com.transcargo.util.Normalizer" %>
<%@ page import="com.transcargo.data.Cache" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.StringTokenizer" %>
<%@include file="header.jsf"%>
<%@include file="session.jsf"%>
<%
    if (User_Id <= 0) {
        response.sendRedirect("auth.jsp");
        return;
    }
%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  	<head><title>TransCargo</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<link type="text/css" href="transcargo.css" rel="stylesheet"/>
  </head>
  <body>
    <table cellpadding="0" cellspacing="0" border="0" width="100%">
        <tr><td>
			<%@include file="page_header.jsf"%>
        </td></tr>
        <tr><td align="center">
<%
		if (request.getParameter("i") != null && request.getParameter("i").equals("1")) out.println("<font color=\"blue\"><b>Предложение добавлено</b></font><br><br>");
		if (request.getParameter("i") != null && request.getParameter("i").equals("2")) out.println("<font color=\"blue\"><b>Предложение изменено</b></font><br><br>");
%>
            Список заявок <b><%=user.getName()%></b><br><br>
<%
		list = null;
		list = dbase.execute("SELECT (SELECT count(id) from posted_transport WHERE (user_id mod 100000000)=" + Integer.toString(User_Id) + "),(SELECT count(id) FROM posted_cargo WHERE (user_id mod 100000000)=" + Integer.toString(User_Id)+")");
		if (list != null) iter = list.iterator();
		String[] rez = iter.next();
		if (Integer.parseInt(rez[1]) > 6) {
			%>[<a href="#cargo">К грузам</a>]<%
		}
//	предложения по транспорту		
		if (Integer.parseInt(rez[0]) > 0) {
			list = dbase.execute("SELECT date_posted, " +
					"date_load, " +
	        		"date_unload, " +
	        		"country_id_from, " +
	        		"region_id_from, " +
	        		"city_id_from, " +
	        		"city_value_from, " +
	        		"country_id_to, " +
	        		"region_id_to, " +
	        		"city_id_to, " +
	        		"city_value_to, " +
	        		"type, " +
	        		"loading, " +
	        		"height, " +
	        		"width, " +
	        		"length, " +
	        		"tir, " +
	        		"ekmt, " +
	        		"adr, " +
	        		"tonnage, " +
	        		"volume, " +
	        		"number, " +
	        		"payment_bid, " +
	        		"payment_type, " +
	        		"payment_currency, " +
	        		"urgent, " +
	        		"urgent_time, " +
	        		"passing, " +
	        		"comments, " +
	        		"permanent, " + 
	        		"id FROM posted_transport WHERE (user_id mod 100000000)=" + Integer.toString(User_Id) + " ORDER BY id DESC");
			if (list != null) iter = list.iterator();
			if (iter.hasNext()) {
%>
				Найдено предложений по транспорту: <%=list.size()%><br><br>
				<table border="0" cellspacing="1" cellpadding="0" width="100%" bgcolor="#000000">
				<tr bgcolor="#A0A0A0"><th rowspan="2">Страна</th>
					<th rowspan="2">Дата</th>
					<th colspan="2">Маршрут</th>
					<th rowspan="2">Кузов</th>
					<th rowspan="2">Детали</th>
					<th rowspan="2">&nbsp;</th>
				</tr>
				<tr bgcolor="#A0A0A0"><th>Загрузка</th><th>Разгрузка</th></tr>
<%
		    	while (iter.hasNext()) {
		    		String[] res = iter.next();
%>
				<tr bgcolor="#ffffff"><td rowspan="2"><b><%
					out.print(Cache.getCountry(Integer.parseInt(res[3])).getName());
				%></b><br><br>Размещено:<br><%=Normalizer.DenormalizeTime(res[0])%></td>
				<td rowspan="2" ><%
				if (res[29].equals("1")) {
					%><b>Постоянно</b><%
				} else {
				%><%=Normalizer.DenormalizeDate(res[1])%><% 
					if (!res[1].equals(res[2])) out.print(" - " + Normalizer.DenormalizeDate(res[2]));
					if (res[25].equals("1")) out.print("<br><b>Срочно! Загрузить до " + res[26] + "</b>");
				} 
				%></td>
				<td><%=res[6]%><br><%
					if (res[4].equals("0")) out.print(Cache.getCountry(Integer.parseInt(res[3])).getName());
					else out.print(Cache.getCountry(Integer.parseInt(res[3])).getRegion(Integer.parseInt(res[4])).getName() + "<br>" + Cache.getCountry(Integer.parseInt(res[3])).getName()); 
				%></td>
				<td><%
					if (res[10]== null || res[10].length() == 0) { 
						if (res[7] == null || res[7].equals("0")) out.print("(Любое направление)"); else {
							if (res[10].length() > 0) out.print(res[10] + "<br>");
							if (res[8] != null && res[8].equals("0")) 
								out.print(Cache.getCountry(Integer.parseInt(res[7])).getName() + "<br>(Любой регион)");
							else 
								out.print(Cache.getCountry(Integer.parseInt(res[7])).getRegion(Integer.parseInt(res[8])).getName() + "<br>" + Cache.getCountry(Integer.parseInt(res[7])).getName()); 
						}
					} else {
						out.print(res[10]+"<br>");
						if (res[8] == null || res[8].equals("0")) 
							out.print(Cache.getCountry(Integer.parseInt(res[7])).getName());
						else 
							out.print(Cache.getCountry(Integer.parseInt(res[7])).getName() + ", " + Cache.getCountry(Integer.parseInt(res[7])).getRegion(Integer.parseInt(res[8])).getName()); 
					}
					if (res[27].equals("1")) out.print("<br><b>Попутный транспорт</b>"); 
				%></td>
				<td><%=Cache.getTransportTypes().get(res[11])%> <%=res[19]%> т./<%=res[20]%> м<sup>3</sup><br>
				загрузка <%
					StringTokenizer tok = new StringTokenizer(res[12], ",");
					while (tok.hasMoreTokens()) {
						out.print(Cache.getTransportLoadings().get(tok.nextToken()));
						if (tok.hasMoreTokens()) out.print(", ");
					}
				%></td><td>
				<% 
					if (!res[13].equals("0") && !res[14].equals("0") && !res[15].equals("0")) out.print("Габариты (д/ш/в): " + res[15] + "м/" + res[14] + "м/" + res[13] + " <br>");
					if (res[16].equals("1")) out.print("[TIR] ");
					if (res[17].equals("1")) out.print("[EKMT] ");
					if (!res[18].equals("0")) out.print("[Класс&nbsp;ADR:&nbsp;" + res[18]+"]"); 
				%>
				</td><td rowspan="2">
				<a href="freightdetails.jsp?id=<%=res[30]%>">Подробнее</a><br>
				<a href="editpostfreight.jsp?id=<%=res[30]%>">Изменить предложение</a><br>
				<a href="DelPostFreightAction?id=<%=res[30]%>">Удалить предложение</a><br>
				</td></tr>
				<tr bgcolor="#ffffff"><td colspan="4" align="left"><% 
					if (res[22].length() > 0 && !res[24].equals("0")) {
						if (res[22].equals("0"))
							out.print("Форма оплаты: " + Cache.getPaymentTypes().get(res[23]) + " " + res[28]);
						else
							out.print("Ставка " + res[22] + " " + Cache.getCurrencies().get(res[24]) + ", Форма оплаты: " + Cache.getPaymentTypes().get(res[23]) + " " + res[28]);
					}
					else out.print("Ставка договорная, Форма оплаты: " + Cache.getPaymentTypes().get(res[23]) + ". " + res[28]);
				%></td></tr>
				
				
<%		    	
					if (iter.hasNext()) {
%>
				<tr bgcolor="#EEEEEE"><td colspan="7" align="left">&nbsp;</td></tr> 
<%					
					}
		    	}
%>
				</table><br>
<%
			}
		}
//		предложения по грузу		
		if (Integer.parseInt(rez[1]) > 0) {
			list = null;
			list = dbase.execute("SELECT date_posted, " +
					"date_load, " +
	        		"date_unload, " +
	        		"country_id_from, " +
	        		"region_id_from, " +
	        		"city_id_from, " +
	        		"city_value_from, " +
	        		"country_id_to, " +
	        		"region_id_to, " +
	        		"city_id_to, " +
	        		"city_value_to, " +
	        		"cargo, " +
	        		"cars, " +
	        		"type, " +
	        		"loading, " +
	        		"tir, " +
	        		"adr, " +
	        		"weight, " +
	        		"volume, " +
	        		"payment_bid, " +
	        		"payment_type, " +
	        		"payment_currency, " +
	        		"urgent, " +
	        		"urgent_time, " +
	        		"combined, " +
	        		"passing, " +
	        		"comments, " +
	        		"permanent, " + 
	        		"times, " + 
	        		"id FROM posted_cargo WHERE (user_id mod 100000000)=" + Integer.toString(User_Id) + " ORDER BY id DESC");
			if (list != null) iter = list.iterator();
			if (iter.hasNext()) {
%>
			<a name="cargo">
				Найдено предложений по грузам: <%=list.size()%></a><br><br>
				<table border="0" cellspacing="1" cellpadding="0" width="100%" bgcolor="#000000">
				<tr bgcolor="#A0A0A0"><th rowspan="2">Страна</th>
					<th rowspan="2">Дата</th>
					<th colspan="2">Маршрут</th>
					<th rowspan="2">Груз</th>
					<th rowspan="2">Кузов</th>
					<th rowspan="2">&nbsp;</th>
				</tr>
				<tr bgcolor="#A0A0A0"><th>Загрузка</th><th>Разгрузка</th></tr>
<%
		    	while (iter.hasNext()) {
		    		String[] res = iter.next();
%>
				<tr bgcolor="#ffffff"><td rowspan="2"><b><%
					out.print(Cache.getCountry(Integer.parseInt(res[3])).getName()); 
				%></b><br><br>Размещено:<br><%=Normalizer.DenormalizeTime(res[0])%></td>
				<td rowspan="2" ><% 
				if (res[27].equals("1")) out.print("Постоянно, " + res[28] + " раз(а) в неделю"); else {
					out.print(Normalizer.DenormalizeDate(res[1])); 
					if (!res[1].equals(res[2])) out.print(" - " + Normalizer.DenormalizeDate(res[2]));
					if (res[22].equals("1")) out.print("<br><b>Срочно! Загрузить до " + res[23] + "</b>");
				}
				%></td>
				<td><%=res[6]%><br><%
					if (res[4].equals("0")) out.print(Cache.getCountry(Integer.parseInt(res[3])).getName());
					else out.print(Cache.getCountry(Integer.parseInt(res[3])).getName() + ", " + Cache.getCountry(Integer.parseInt(res[3])).getRegion(Integer.parseInt(res[4])).getName()); 
				%></td>
				<td><%
					out.print(res[10]+"<br>");
					if (res[8] == null || res[8].equals("0")) 
						out.print(Cache.getCountry(Integer.parseInt(res[7])).getName());
					else 
						out.print(Cache.getCountry(Integer.parseInt(res[7])).getName() + ", " + Cache.getCountry(Integer.parseInt(res[7])).getRegion(Integer.parseInt(res[8])).getName()); 
					if (res[25].equals("1")) out.print("<br><b>Попутный груз</b>"); 
				%></td>
				<td><%=res[11] %><% if (res[24].equals("1")) out.print("<br><b>Сборный груз</b>");  %></td>
				<td><%=Cache.getTransportTypes().get(res[13])%> <%=res[17]%> т./<%=res[18]%> м<sup>3</sup><% if (Integer.parseInt(res[12]) > 1) out.print(", требуется <b>" + res[12] + "</b> машин(ы)"); %><br>
				загрузка <%
					StringTokenizer tok = new StringTokenizer(res[14], ",");
					while (tok.hasMoreTokens()) {
						out.print(Cache.getTransportLoadings().get(tok.nextToken()));
						if (tok.hasMoreTokens()) out.print(", ");
					}
				%><br><% 
					if (res[15].equals("1")) out.print("[TIR] ");
					if (!res[16].equals("0")) out.print("[Класс ADR: " + res[16]+"]"); 
				%>
				</td><td rowspan="2">
				<a href="cargodetails.jsp?id=<%=res[29]%>">Подробнее</a><br>
				<a href="editpostcargo.jsp?id=<%=res[29]%>">Изменить предложение</a><br>
				<a href="DelPostCargoAction?id=<%=res[29]%>">Удалить предложение</a><br>
				</td></tr>
				<tr bgcolor="#ffffff"><td colspan="4" align="left"><% 
					if (res[19].length() > 0 && !res[21].equals("0")) {
						if (res[19].equals("0"))
							out.print("Форма оплаты: " + Cache.getPaymentTypes().get(res[20]) + " " + res[26]);
						else
							out.print("Ставка " + res[19] + " " + Cache.getCurrencies().get(res[21]) + ", Форма оплаты: " + Cache.getPaymentTypes().get(res[20]) + " " + res[26]);
					}
					else out.print("Ставка договорная, Форма оплаты: " + Cache.getPaymentTypes().get(res[20]) + " " + res[26]);
				%></td></tr>
				
				
<%		    	
					if (iter.hasNext()) {
%>
				<tr bgcolor="#EEEEEE"><td colspan="7" align="left">&nbsp;</td></tr> 
<%					
					}
		    	}
%>
				</table>
<%
			}
		}
%>
        </td></tr>
        </table>
  </body>
</html>
